package com.koron.operation.classes.schedule.service.impl;


import cn.hutool.core.util.StrUtil;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.operation.classes.schedule.bean.dto.ClassesShiftDTO;
import com.koron.operation.classes.schedule.bean.dto.ClassesShiftScheduleDTO;
import com.koron.operation.classes.schedule.bean.dto.ShiftStaffDTO;
import com.koron.operation.classes.schedule.bean.po.ClassesShift;
import com.koron.operation.classes.schedule.bean.query.ShiftStaffQuery;
import com.koron.operation.classes.schedule.mapper.ClassesShiftMapper;
import com.koron.operation.classes.schedule.service.ClassesShiftService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 排班班次(ClassesShift)表服务实现类
 *
 * @author jzp
 * @since 2021-10-20 16:20:36
 */
@Service
public class ClassesShiftServiceImpl implements ClassesShiftService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public ClassesShift getById(SessionFactory factory, String id) {
        ClassesShiftMapper mapper = factory.getMapper(ClassesShiftMapper.class);
        return mapper.selectById(id);
    }

    @Override
    @TaskAnnotation("getDtoById")
    public ClassesShiftDTO getDtoById(SessionFactory factory, String id) {
        ClassesShiftMapper mapper = factory.getMapper(ClassesShiftMapper.class);
        return mapper.selectDtoById(id);
    }

    @Override
    @TaskAnnotation("listDtoByIds")
    public List<ClassesShiftDTO> listDtoByIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return null;
        }
        ClassesShiftMapper mapper = factory.getMapper(ClassesShiftMapper.class);
        return mapper.selectDtoByIds(ids);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<ClassesShift> list(SessionFactory factory, ClassesShift classesShift) {
        ClassesShiftMapper mapper = factory.getMapper(ClassesShiftMapper.class);
        return mapper.selectList(classesShift);
    }

    @Override
    @TaskAnnotation("listShiftScheduleDTOByIds")
    public List<ClassesShiftScheduleDTO> listShiftScheduleDTOByIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return null;
        }
        ClassesShiftMapper mapper = factory.getMapper(ClassesShiftMapper.class);
        return mapper.selectShiftDtoByIds(ids);
    }

    /**
     * 查询当前人员值班天数
     */
    @Override
    @TaskAnnotation("listDto")
    public List<ShiftStaffDTO> listDto(SessionFactory factory, ShiftStaffQuery query) {
        if (query.getStaffBy() == null) {
            query.setStaffBy(ThreadLocalContext.get().getAccount());
        }
        ClassesShiftMapper mapper = factory.getMapper(ClassesShiftMapper.class);
        return mapper.selectDtoList(query);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, ClassesShift classesShift) {
        ClassesShiftMapper mapper = factory.getMapper(ClassesShiftMapper.class);
        return mapper.insert(classesShift) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<ClassesShift> classesShifts) {
        if (CollectionUtils.isEmpty(classesShifts)) {
            return false;
        }
        ClassesShiftMapper mapper = factory.getMapper(ClassesShiftMapper.class);
        return mapper.insertOrUpdateBatch(classesShifts) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, ClassesShift classesShift) {
        ClassesShiftMapper mapper = factory.getMapper(ClassesShiftMapper.class);
        return mapper.update(classesShift) > 0;
    }

    @Override
    @TaskAnnotation("updateState")
    public boolean updateState(SessionFactory factory, String shiftId, Integer status) {
        ClassesShift classesShift = new ClassesShift();
        classesShift.setId(shiftId);
        classesShift.setStatus(status);
        return update(factory, classesShift);
    }

    @Override
    @TaskAnnotation("updateBatchState")
    public boolean updateBatchState(SessionFactory factory, List<String> shiftIds, Integer status) {
        ClassesShiftMapper mapper = factory.getMapper(ClassesShiftMapper.class);
        return mapper.updateBatchStatus(shiftIds, status) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        ClassesShiftMapper mapper = factory.getMapper(ClassesShiftMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByScheduleId")
    public boolean removeByScheduleId(SessionFactory factory, String scheduleId) {
        if (StrUtil.isEmpty(scheduleId)) {
            return false;
        }
        ClassesShift classesShift = new ClassesShift();
        classesShift.setScheduleId(scheduleId);
        List<ClassesShift> list = list(factory, classesShift);

        return deleteBatchIds(factory, list.stream().map(ClassesShift::getId).collect(Collectors.toList()));
    }
}
